package cn.yuan.xiaoyu.newsmodule.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.config.Constant;
import cn.yuan.xiaoyu.commonmodule.utils.DateUtils;
import cn.yuan.xiaoyu.commonmodule.utils.L;
import cn.yuan.xiaoyu.commonmodule.utils.ListUtils;
import cn.yuan.xiaoyu.newsmodule.bean.TouTiaolistBean;


/**
 * Created by yukuoyuan on 2017/3/7.
 * 这是一个新闻列表数据的适配器
 */

public class NewsListDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<TouTiaolistBean.DataBean> list;
    Context context;
    OnItemClickListener onItemClickListener;

    public NewsListDataAdapter(List<TouTiaolistBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onNewsItemClick(String source_url);

        void onVideoItemClick(String source_url);

    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).article_genre.equals(Constant.ARTICLE_GENRE_ARTICLE)) {
            if (ListUtils.isEmpty(list.get(position).image_list)) {
                return Constant.SENDTXT;
            } else {
                return Constant.SENDIMAGE;
            }
        } else if (list.get(position).article_genre.equals(Constant.ARTICLE_GENRE_GALLERY)) {
            return Constant.FROMTXT;
        } else if (list.get(position).article_genre.equals(Constant.ARTICLE_GENRE_VIDEO)) {
            return Constant.FROMIMAGE;
        }

        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == Constant.SENDTXT) {
            //发送者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_list_img, parent, false);
            return new IMGHolder(view);
        } else if (viewType == Constant.SENDIMAGE) {
            //发送者图片
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_list_bigimg, parent, false);
            return new BIGIMGHolder(view);
        } else if (viewType == Constant.FROMTXT) {
            //接受者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_list_gallery, parent, false);
            return new GALLERYHolder(view);
        } else if (viewType == Constant.FROMIMAGE) {
            //接受者图片
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_list_video, parent, false);
            return new VIDEOHolder(view);
        }
        return null;
    }
//
//    @Override
//    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = View.inflate(parent.getContext(), R.layout.item_news_list, null);
//        return new BaseViewHolder(view);
//    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (list.get(position).article_genre.equals(Constant.ARTICLE_GENRE_ARTICLE)) {
            //文章类型
            if (list.get(position).image_list == null || list.get(position).image_list.size() == 0) {
                if (!TextUtils.isEmpty(list.get(position).image_url)) {
                    //单图片文章
                    Glide.with(context).load(list.get(position).image_url).into(((IMGHolder) holder).ivRightImg1);
                    ((IMGHolder) holder).rlRightImg.setVisibility(View.VISIBLE);
                    ((IMGHolder) holder).viewFill.setVisibility(View.VISIBLE);
                    /**
                     * 设置标题
                     */
                    ((IMGHolder) holder).tvTitle.setText(list.get(position).title);
                    /**
                     * 设置作者
                     */
                    ((IMGHolder) holder).tvAuthorName.setText(list.get(position).source);
                    /**
                     * 设置评论
                     */
                    ((IMGHolder) holder).tvCommentCount.setText(list.get(position).comments_count + "评论");
                    /**
                     * 设置时间
                     */
                    long time = list.get(position).behot_time * 1000l;
                    ((IMGHolder) holder).tvTime.setText(DateUtils.getFormatDate(time, "yyyy-MM-dd"));
                    ((IMGHolder) holder).ll_news_list_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onItemClickListener != null) {
                                onItemClickListener.onNewsItemClick(list.get(position).source_url);
                            }
                        }
                    });
                }
            } else {
                //3张图片
                ((BIGIMGHolder) holder).llCenterImg.setVisibility(View.VISIBLE);
                Glide.with(context).load(list.get(position).image_list.get(0).url).into(((BIGIMGHolder) holder).ivCenterImg1);
                Glide.with(context).load(list.get(position).image_list.get(1).url).into(((BIGIMGHolder) holder).ivCenterImg2);
                Glide.with(context).load(list.get(position).image_list.get(2).url).into(((BIGIMGHolder) holder).ivCenterImg3);
                /**
                 * 设置标题
                 */
                ((BIGIMGHolder) holder).tvTitle.setText(list.get(position).title);
                /**
                 * 设置作者
                 */
                ((BIGIMGHolder) holder).tvAuthorName.setText(list.get(position).source);
                /**
                 * 设置评论
                 */
                ((BIGIMGHolder) holder).tvCommentCount.setText(list.get(position).comments_count + "评论");
                /**
                 * 设置时间
                 */
                long time = list.get(position).behot_time * 1000l;
                ((BIGIMGHolder) holder).tvTime.setText(DateUtils.getFormatDate(time, "yyyy-MM-dd"));
                ((BIGIMGHolder) holder).ll_news_list_bigimg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onNewsItemClick(list.get(position).source_url);
                        }
                    }
                });
            }
        } else if (list.get(position).article_genre.equals(Constant.ARTICLE_GENRE_GALLERY)) {
            //画廊类型
            if (list.get(position).image_list == null) {
                Glide.with(context).load(list.get(position).image_url).into(((GALLERYHolder) holder).ivRightImg1);
                ((GALLERYHolder) holder).rlRightImg.setVisibility(View.VISIBLE);
                ((GALLERYHolder) holder).viewFill.setVisibility(View.VISIBLE);
            } else {
                Glide.with(context).load(list.get(position).image_list.get(0).url).into(((GALLERYHolder) holder).ivBigImg);
                ((GALLERYHolder) holder).rlBigImg.setVisibility(View.VISIBLE);
                ((GALLERYHolder) holder).tvImgCount.setText(list.get(position).image_list.size() + "图");
            }
            /**
             * 设置标题
             */
            ((GALLERYHolder) holder).tvTitle.setText(list.get(position).title);
            /**
             * 设置作者
             */
            ((GALLERYHolder) holder).tvAuthorName.setText(list.get(position).source);
            /**
             * 设置评论
             */
            ((GALLERYHolder) holder).tvCommentCount.setText(list.get(position).comments_count + "评论");
            /**
             * 设置时间
             */
            long time = list.get(position).behot_time * 1000l;
            ((GALLERYHolder) holder).tvTime.setText(DateUtils.getFormatDate(time, "yyyy-MM-dd"));
            ((GALLERYHolder) holder).ll_news_list_gallery.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onNewsItemClick(list.get(position).source_url);
                    }
                }
            });

        } else if (list.get(position).article_genre.equals(Constant.ARTICLE_GENRE_VIDEO)) {
            //视频类型
            Glide.with(context).load(list.get(position).image_url).into(((VIDEOHolder) holder).ivRightImg1);
            ((VIDEOHolder) holder).rlRightImg.setVisibility(View.VISIBLE);
            ((VIDEOHolder) holder).viewFill.setVisibility(View.VISIBLE);
            ((VIDEOHolder) holder).llVideo.setVisibility(View.VISIBLE);
            ((VIDEOHolder) holder).tvDuration.setText(list.get(position).video_duration_str);
            /**
             * 设置标题
             */
            ((VIDEOHolder) holder).tvTitle.setText(list.get(position).title);
            /**
             * 设置作者
             */
            ((VIDEOHolder) holder).tvAuthorName.setText(list.get(position).source);
            /**
             * 设置评论
             */
            ((VIDEOHolder) holder).tvCommentCount.setText(list.get(position).comments_count + "评论");
            /**
             * 设置时间
             */
            long time = list.get(position).behot_time * 1000l;
            ((VIDEOHolder) holder).tvTime.setText(DateUtils.getFormatDate(time, "yyyy-MM-dd"));
            ((VIDEOHolder) holder).ll_news_list_video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onVideoItemClick(list.get(position).source_url);
                    }
                }
            });
            L.d("时间戳", time + "");
        }


    }

    @Override
    public int getItemCount() {
        if (ListUtils.isEmpty(list)) {
            return 0;
        }
        return list.size();
    }

    public void addData(List<TouTiaolistBean.DataBean> data) {
        this.list.addAll(data);
        notifyDataSetChanged();
    }


    class IMGHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle;
        private final TextView tvAuthorName;
        private final TextView tvCommentCount;
        private final TextView tvTime;
        private final RelativeLayout rlRightImg;
        private final ImageView ivRightImg1;
        private final View viewFill;
        private final LinearLayout ll_news_list_img;

        public IMGHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvAuthorName = (TextView) itemView.findViewById(R.id.tvAuthorName);
            tvCommentCount = (TextView) itemView.findViewById(R.id.tvCommentCount);
            tvTime = (TextView) itemView.findViewById(R.id.tvTime);
            ivRightImg1 = (ImageView) itemView.findViewById(R.id.ivRightImg1);
            rlRightImg = (RelativeLayout) itemView.findViewById(R.id.rlRightImg);
            viewFill = itemView.findViewById(R.id.viewFill);
            ll_news_list_img = (LinearLayout) itemView.findViewById(R.id.ll_news_list_img);
        }
    }

    class BIGIMGHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle;
        private final TextView tvAuthorName;
        private final TextView tvCommentCount;
        private final TextView tvTime;
        private final ImageView ivCenterImg1;
        private final ImageView ivCenterImg2;
        private final ImageView ivCenterImg3;

        private final LinearLayout llCenterImg;
        private final LinearLayout ll_news_list_bigimg;

        public BIGIMGHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvAuthorName = (TextView) itemView.findViewById(R.id.tvAuthorName);
            tvCommentCount = (TextView) itemView.findViewById(R.id.tvCommentCount);
            tvTime = (TextView) itemView.findViewById(R.id.tvTime);
            llCenterImg = (LinearLayout) itemView.findViewById(R.id.llCenterImg);
            ivCenterImg1 = (ImageView) itemView.findViewById(R.id.ivCenterImg1);
            ivCenterImg2 = (ImageView) itemView.findViewById(R.id.ivCenterImg2);
            ivCenterImg3 = (ImageView) itemView.findViewById(R.id.ivCenterImg3);
            ll_news_list_bigimg = (LinearLayout) itemView.findViewById(R.id.ll_news_list_bigimg);
        }
    }

    class GALLERYHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle;
        private final TextView tvAuthorName;
        private final TextView tvCommentCount;
        private final TextView tvTime;
        private final RelativeLayout rlRightImg;
        private final ImageView ivRightImg1;
        private final View viewFill;
        private final ImageView ivBigImg;
        private final RelativeLayout rlBigImg;
        private final TextView tvImgCount;
        private final LinearLayout ll_news_list_gallery;

        public GALLERYHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvAuthorName = (TextView) itemView.findViewById(R.id.tvAuthorName);
            tvCommentCount = (TextView) itemView.findViewById(R.id.tvCommentCount);
            tvTime = (TextView) itemView.findViewById(R.id.tvTime);
            ivRightImg1 = (ImageView) itemView.findViewById(R.id.ivRightImg1);
            rlRightImg = (RelativeLayout) itemView.findViewById(R.id.rlRightImg);
            viewFill = itemView.findViewById(R.id.viewFill);
            rlBigImg = (RelativeLayout) itemView.findViewById(R.id.rlBigImg);
            ivBigImg = (ImageView) itemView.findViewById(R.id.ivBigImg);
            tvImgCount = (TextView) itemView.findViewById(R.id.tvImgCount);
            ll_news_list_gallery = (LinearLayout) itemView.findViewById(R.id.ll_news_list_gallery);
        }
    }

    class VIDEOHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle;
        private final TextView tvAuthorName;
        private final TextView tvCommentCount;
        private final TextView tvTime;
        private final LinearLayout llVideo;
        private final TextView tvDuration;
        private final RelativeLayout rlRightImg;
        private final ImageView ivRightImg1;
        private final View viewFill;
        private final LinearLayout ll_news_list_video;

        public VIDEOHolder(View itemView) {
            super(itemView);
            ivRightImg1 = (ImageView) itemView.findViewById(R.id.ivRightImg1);
            rlRightImg = (RelativeLayout) itemView.findViewById(R.id.rlRightImg);
            viewFill = itemView.findViewById(R.id.viewFill);
            tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            tvAuthorName = (TextView) itemView.findViewById(R.id.tvAuthorName);
            tvCommentCount = (TextView) itemView.findViewById(R.id.tvCommentCount);
            tvTime = (TextView) itemView.findViewById(R.id.tvTime);
            llVideo = (LinearLayout) itemView.findViewById(R.id.llVideo);
            tvDuration = (TextView) itemView.findViewById(R.id.tvDuration);
            ll_news_list_video = (LinearLayout) itemView.findViewById(R.id.ll_news_list_video);
        }
    }
}
